/**
 * author      : 吴中勤
 * date        : 2018/8/11
 * description : 日志存储接口
 */

package com.deepexi.adapter.log;

import com.deepexi.adapter.log.persist.RequestLogMapper;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

@Component
public class LogRecorderImpl implements LogRecorder {

     private final Logger log = LoggerFactory.getLogger(this.getClass());

    private static final int THREAD_POOL_SIZE = 3;
    private ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("log-thread-%d").build();
    private ExecutorService executor = Executors.newFixedThreadPool(THREAD_POOL_SIZE, namedThreadFactory);

    @Autowired
    private RequestLogMapper requestLogMapper;

    @Override
    public void record(Log4Request log) {
//         requestLogMapper.insert(log);
        if (null == log) return;
        executor.submit(() -> {
            requestLogMapper.insert(log);
        });
    }

}
